﻿using System.Data;
using DataLogical;
using DataLogical.TableClass;
using Biz.Utility;

namespace Biz.UserManagement
{
    public class Employee
    {
        private int id = Define.NEW;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string userCName;

        public string UserCName
        {
            get { return userCName; }
            set { userCName = value; }
        }
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string division;

        public string Division
        {
            get { return division; }
            set { division = value; }
        }
        private string department;

        public string Department
        {
            get { return department; }
            set { department = value; }
        }
        private int status;

        public int Status
        {
            get { return status; }
            set { status = value; }
        }
        private int defaultApproveID;

        public int DefaultApproveID
        {
            get { return defaultApproveID; }
            set { defaultApproveID = value; }
        }
        private string employeeCode = string.Empty;

        public string EmployeeCode
        {
            get { return employeeCode; }
            set { employeeCode = value; }
        }


        private RoleList roleList = null;

        public Employee(int _id)
        {
            tblUser u = new tblUser(_id);
            Id = _id;
            UserCName = u.UserCName;
            UserName = u.UserName;
            Division = u.Division;
            Department = u.Department;
            DefaultApproveID = u.DefaultApprover;
            status = u.Status;
            EmployeeCode = u.EmployeeCode;
            roleList = new RoleList(_id);
        }

        public Employee()
        {
            Id = Define.NEW;
            roleList = new RoleList();
        }

        public void RoleAdd(Role.RoleType _roleType)
        {
            roleList.RoleAdd(_roleType);
        }

        public void RoleDelete(Role.RoleType _roleType)
        {
            roleList.RoleDelete(_roleType);
        }

        public bool HasRole(Role.RoleType _roleType)
        {
            return roleList.HasRole(_roleType);
        }

        public DataTable ListRole()
        {
            return roleList.ListRole();
        }

        public void Save()
        {
            tblUser u;
            if (Id == Define.NEW)
                u = new tblUser();
            else
                u = new tblUser(Id);
            u.UserName = UserName;
            u.UserCName = UserCName;
            u.Division = Division;
            u.Department = Department;
            u.DefaultApprover = DefaultApproveID;
            u.Status = Status;
            u.EmployeeCode = EmployeeCode;
            roleList.UserID = Id;
            roleList.Save();
            if (Id == Define.NEW)
                u.Insert(u);
            else
                u.Update(u);
        }

        public static Employee GetEmployee(string _userID)
        {
            return new Employee(User.GetUSER_ID(_userID));
        }
        
    }
}
